package com.lhkj.ct.admin.modules.common.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.file.FileUploadUtils;
import com.lhkj.ct.base.utils.file.MimeTypeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author : hwx
 * @version ：2.0
 * @Description : 文件管理器
 * @date : 2024-04-09
 */
@RestController
@RequestMapping("api/system/file")
@Api(tags = "1.文件管理器")
@ApiSupport(order = 0)
@Validated
public class FileManageController {

    @ApiOperation(value = "上传文件", notes = "不限类型,格式受后端控制")
    @ApiOperationSupport(order = 1)
    @PostMapping("/upload")
    public ActionResult<?> upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(file);
            return ActionResult.success(avatar);
        }
        return ActionResult.fail("上传异常，请联系管理员");
    }

    @ApiOperation(value = "上传图片", notes = "格式为bmp, gif, jpg, jpeg, png")
    @ApiOperationSupport(order = 2)
    @PostMapping("/upload/image")
    public ActionResult<?> uploadImg(@RequestParam("file") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(FileUploadUtils.getDefaultBaseDir(), file, MimeTypeUtils.IMAGE_EXTENSION);
            return ActionResult.success(avatar);
        }
        return ActionResult.fail("上传异常，请联系管理员");
    }

    @ApiOperation(value = "上传文档", notes = "doc, docx, xls, xlsx, ppt, pptx, pdf文档")
    @ApiOperationSupport(order = 2)
    @PostMapping("/upload/document")
    public ActionResult<?> uploadDocument(@RequestParam("file") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(FileUploadUtils.getDefaultBaseDir(), file, MimeTypeUtils.DOCUMENT_EXTENSION);
            return ActionResult.success(avatar);
        }
        return ActionResult.fail("上传异常，请联系管理员");
    }
}
